<?php
namespace Home\Model;
use Think\Model;
class AddressModel extends Model{
    //搜索全部地址信息；
	 public  function allAddress($id,$idd=""){
             if($idd){
                //编辑地址时返回的一个地址的全部信息
                $where['user_id']=$id;
                $result=$this->alias('address')
                     ->join('left join shop_provinces as pro on address.province_id=pro.provinceid')
                     ->join('left join shop_cities as city on address.city_id=city.cityid')
                     ->join('left join shop_areas as area on address.area_id=area.areaid')
                     ->join('left join shop_user as user on address.user_id=user.id')
                     ->where($where)
                     ->where('address.id='.$idd)
                     ->field('receive_person,address.province_id,address.city_id,address.area_id,province,city,area,receive_mobile,detail_address,address.id,isdefault')
                     ->select();
                }else{
                //返回全部地址的信息；
                 $where['user_id']=$id;
                 $result=$this->alias('address')
                     ->join('left join shop_provinces as pro on address.province_id=pro.provinceid')
                     ->join('left join shop_cities as city on address.city_id=city.cityid')
                     ->join('left join shop_areas as area on address.area_id=area.areaid')
                     ->join('left join shop_user as user on address.user_id=user.id')
                     ->where($where)
                     ->field('receive_person,address.province_id,address.city_id,address.area_id,province,city,area,receive_mobile,detail_address,address.id,isdefault')
                     ->select();
                
             }
            return  $result;
        
    }
    //删除一个地址
    public function deleteAdd($id){
        $result=$this->where('id='.$id)
                     ->delete();
        return $result;
    }
    //设默认地址，点击时一个地址默认为1，其它默认为0
    public function updateDe($arr){
        $where['user_id']=$arr['user_id'];
        $where1['user_id']=$arr['user_id'];
        $where1['id']=$arr['id'];
        $data['isdefault']=0;
        $data1['isdefault']=1;
        $result=$this->where($where)
                     ->where($data1)
                     ->find();
        if($result){
            $result1=$this->where($where)
                          ->save($data);
            if($result1){
                $result2=$this->where($where1)
                              ->save($data1);
                return $result1.$result2;
            }
        }else{
               $result2=$this->where($where1)
                              ->save($data1);
                return $result2;
        }
                   
    }
    //编辑地址
    public function updateAddress($id,$data){     
           $result=$this->where('id='.$id)
                        ->save($data);
                        // var_dump($result);
                        // die;

           return $result;
    }
}